﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CollapseableControl.cs" company="">
//   
// </copyright>
// <summary>
//   The collapseable control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Controls
{
    using System;

    using Libium.Properties;

    /// <summary>
    /// The collapseable control.
    /// </summary>
    public partial class CollapseableControl : HeadedControl, ICollapsableControl
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CollapseableControl"/> class.
        /// </summary>
        public CollapseableControl()
        {
            InitializeComponent();
            CollapseToolStrip.BackColor = BackColor;
            CollapseToolStrip.Renderer = new ToolStripSystemRenderer();
            //Collapsed = false;
        }

        #endregion

		  private bool _Collapsed = true;

        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether collapsed.
        /// </summary>
        public bool Collapsed
        {
            get
            {
					if (!_Collapsed && !ContentPanel.Visible)
						SetVisibilityOnCollapsed(_Collapsed);
                return !ContentPanel.Visible;
            }

            set
            {
					_Collapsed = value;
					SetVisibilityOnCollapsed(value);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The collapse button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CollapseButton_Click(object sender, EventArgs e)
        {
            Collapsed = !Collapsed;
        }

        #endregion

        private void HeaderPanel_DoubleClick(object sender, EventArgs e)
        {
            Collapsed = !Collapsed;
        }

        private void NameControl_DoubleClick(object sender, EventArgs e)
        {
            Collapsed = !Collapsed;
        }

		  private void SetVisibilityOnCollapsed(bool collapsed)
		  {
			  ContentPanel.Visible = !collapsed;
			  if (ContentPanel.Visible)
			  {
				  Height = HeaderPanel.Height + ContentPanel.Height;
			  }
			  else
			  {
				  Height = HeaderPanel.Height;
			  }

			  CollapseButton.Image = !collapsed ? Resources.opened : Resources.closed;
		  }
    }
}